package com.trassion.flutterhybirddemo


import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.trassion.flutterhybirddemo.databinding.ActivityMainBinding
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterImageView
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint

class MainActivity : AppCompatActivity() {

    lateinit var mViewBinding: ActivityMainBinding;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        val flutterImageView = FlutterView(this)
        val layoutParams =
            FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        addContentView(flutterImageView, layoutParams)
        mViewBinding.button.setOnClickListener {
            //1.打开flutter 默认页面
//            startActivity(
//                FlutterActivity.createDefaultIntent(this)
//            )
            //2. 开启一个新的flutter引擎
//            startActivity(
//                FlutterActivity
//                    .withNewEngine()
//                    .initialRoute("page_one")
//                    .build(this)
//            )

//            //3.使用缓存Flutter引擎
            startActivity(
                FlutterActivity
                    .withCachedEngine("myFlutterEngine1")
                    .build(this)
            )

//            4.
//            //3.使用缓存Flutter引擎
//            val engine = FlutterEngineCache.getInstance()["myFlutterEngine"]
//            engine?.navigationChannel?.pushRoute("page_two")

        }

        val engine = FlutterEngineCache.getInstance()["myFlutterEngine1"]
        engine?.plugins?.add(MyPlugin())

        mViewBinding.openFlutterFragment.setOnClickListener {
            startActivity(Intent(this, FlutterFragmentActivity::class.java))
        }

        mViewBinding.openFlutterView.setOnClickListener {
            startActivity(Intent(this, FlutterViewActivity::class.java))
        }

    }
}